/*
 * @(#) KSystemUtil.java 2016-2-2
 * 
 * Copyright (c) 2010, HMI Technology. All Rights Reserved. HMI Technology. CONFIDENTIAL
 */
package com.webkitlib.util;


import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.webkitlib.base.BaseApplication;

/**
 * 
 * 系统信息获取
 * 
 * @author 韩伟光
 * @version 1.0
 * @since 2016-2-2
 */
public class KSystemUtil {


    /**
     * 获取版本号
     * 
     * @return 当前应用的版本号
     */
    public static int getVersion() {
        try {
            PackageManager manager = BaseApplication.context().getPackageManager();
            PackageInfo info =
                            manager.getPackageInfo(BaseApplication.context().getPackageName(), 0);
            int version = info.versionCode;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取版本名
     * 
     * @return 用户显示的版本名称
     */
    public static String getVersionName() {
        try {
            PackageManager manager = BaseApplication.context().getPackageManager();
            PackageInfo info =
                            manager.getPackageInfo(BaseApplication.context().getPackageName(), 0);
            String version = info.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取当前时间
     * 
     * @return
     */
    public static String dateFormat() {
        Date date = new Date();
        return dateFormats(date, "yyyy-MM-dd HH:mm:ss");
    }

    @SuppressLint("SimpleDateFormat")
    private static String dateFormats(Date date, String dateFormat) {
        if (date == null)
            return "";
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        {
            return format.format(date);
        }
    }

    /**
     * 指定格式返回当前系统时间
     */
    @SuppressLint("SimpleDateFormat")
    public static String getDataTime(String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(new Date());
    }

    /**
     * 判断是否为wifi联网
     */
    public static boolean isWiFi() {
        ConnectivityManager cm =
                        (ConnectivityManager) BaseApplication.context().getSystemService(
                                        Context.CONNECTIVITY_SERVICE);
        // wifi的状态：ConnectivityManager.TYPE_WIFI
        // 3G的状态：ConnectivityManager.TYPE_MOBILE
        State state = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        return State.CONNECTED == state;
    }

    private static Boolean _isTablet;
    private static double displayDensity;

    /**
     * 判断有无网络
     * 
     * @return
     */
    public static boolean hasInternet() {
        boolean flag;
        if (((ConnectivityManager) BaseApplication.context().getSystemService("connectivity"))
                        .getActiveNetworkInfo() != null)
            flag = true;
        else
            flag = false;
        return flag;
    }

    /**
     * 判断是否是平板电脑
     * 
     * @return
     */
    public static boolean isTablet() {
        if (_isTablet == null) {
            boolean flag;
            if ((0xf & BaseApplication.context().getResources().getConfiguration().screenLayout) >= 3)
                flag = true;
            else
                flag = false;
            _isTablet = Boolean.valueOf(flag);
        }
        return _isTablet.booleanValue();
    }

    public static float getDensity() {
        if (displayDensity == 0.0)
            displayDensity = getDisplayMetrics().density;
        return (float) displayDensity;
    }

    public static DisplayMetrics getDisplayMetrics() {
        DisplayMetrics displaymetrics = new DisplayMetrics();
        ((WindowManager) BaseApplication.context().getSystemService(Context.WINDOW_SERVICE))
                        .getDefaultDisplay().getMetrics(displaymetrics);
        return displaymetrics;
    }

    public static float getScreenWidth() {
        return getDisplayMetrics().widthPixels;
    }

    public static float dpToPixel(float dp) {
        return dp * (getDisplayMetrics().densityDpi / 160F);
    }

    /**
     * 判断是否是飞行模式<br>
     * <a>true 是飞行模式 <a>false为不是飞行模式
     * 
     * @param context
     * @return
     */
    @SuppressWarnings("deprecation")
    public static boolean isAirplaneModeOn() {
        return Settings.System.getInt(BaseApplication.context().getContentResolver(),
                        Settings.System.AIRPLANE_MODE_ON, 0) != 0;
    }

    /**
     * 获取IMEI
     * 
     * @param context
     * @return
     */
    public static String getIMEI() {
        return ((TelephonyManager) BaseApplication.context().getSystemService(
                        Context.TELEPHONY_SERVICE)).getDeviceId();
    }

    public static int getSDKVersion() {
        return android.os.Build.VERSION.SDK_INT;
    }

    /**
     * 获取手机配置信息
     * 
     * @return
     */
    @SuppressWarnings("deprecation")
    public static String getPhoneInfo() {
        StringBuffer sbf = new StringBuffer();
        // android版本号
        sbf.append("系统版本号: ");
        sbf.append("Android:" + Build.VERSION.RELEASE);
        sbf.append("_");
        sbf.append(Build.VERSION.SDK_INT);
        // 手机制造商
        sbf.append("\n");
        sbf.append("手机制造商: ");
        sbf.append(Build.MANUFACTURER);
        sbf.append("\n");
        // 手机型号
        sbf.append("手机型号: ");
        sbf.append(Build.MODEL);
        sbf.append("\n");
        // cpu架构
        sbf.append("CPU架构: ");
        sbf.append(Build.CPU_ABI);
        return sbf.toString();
    }

    /**
     * 获取当前手机号，现在能获取到的很少
     * 
     * @param context
     * @return
     */
    public static String getPhoneNumber() {
        TelephonyManager tManager =
                        (TelephonyManager) BaseApplication.context().getSystemService(
                                        Context.TELEPHONY_SERVICE);
        String number = tManager.getLine1Number();
        return number;
    }

    /**
     * 判断是否为模拟器
     * 
     * @return
     */
    public static boolean isEmulator() {
        return Build.MODEL.equals("sdk") || Build.MODEL.equals("google_sdk");
    }



}
